package com.neusoft.question;
/**
 * 如果 object 是 class 的一个实例，则 instanceof 运算符返回 true。
 * 如果 object 不是指定类的一个实例，或者 object 是 null，则返回 false。
 * @author LYW
 *
 */

interface A{ }
class B implements A{ }
class C extends B { }
public class InstanceofTest {

	public static void main(String[] args) {
		// 
		A a=null;
		B b=null;
		boolean res;
		
		System.out.println("instanceoftest test case 1: ------------------");
		res = a instanceof A;
		System.out.println("a instanceof A: " + res);//null -false
		res = b instanceof B;
		System.out.println("b instanceof B: " + res);//null -false
		
		System.out.println("\ninstanceoftest test case 2: ------------------");
		a=new B();
		b=new B();
		res = a instanceof A;
		System.out.println("a instanceof A: " + res);//true
		res = a instanceof B;
		System.out.println("a instanceof B: " + res);//true
		res = b instanceof A;
		System.out.println("b instanceof A: " + res);//true
		res = b instanceof B;
		System.out.println("b instanceof B: " + res);//true
		System.out.println("\ninstanceoftest test case 3: ------------------");
		B b2=new C();
		res = b2 instanceof A;
		System.out.println("b2 instanceof A: " + res);//true
		res = b2 instanceof B;
		System.out.println("b2 instanceof B: " + res);//true
		res = b2 instanceof C;
		System.out.println("b2 instanceof C: " + res);//true
		
		Object obj = new Object();
		boolean b1 = obj instanceof String;
		System.out.println("obj instanceof String :" +b1);
		String str = new String("ww");
		b1 = str instanceof Object;
		System.out.println("str instanceof Object:" +b1);
	}

}
